package com.cbank.platform.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.cbank.utils.StringUtil;


public class LogInterceptor extends HandlerInterceptorAdapter {

	Logger logger = Logger.getLogger(LogInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
		long startTime = System.currentTimeMillis();
		request.setAttribute("startTime", startTime);
		String uri = request.getRequestURI();
		logger.info("===URI===: " + uri);
		return true;
	}
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

		long startTime = StringUtil.toLongValue(request.getAttribute("startTime"));
		logger.info("URI:" + request.getRequestURI() + " ,cost: "+ (System.currentTimeMillis() - startTime));
	}
	
}